package com.example.bill.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.example.bill.R;
import com.example.bill.adapter.BillListAdapter;
import com.example.bill.adapter.YearMonthAdapter;
import com.example.bill.application.BillApplication;
import com.example.bill.entity.Memo;
import com.example.bill.repository.MemoRepository;

import java.time.LocalDate;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BillQueryPageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BillQueryPageFragment extends Fragment implements AdapterView.OnItemSelectedListener, ViewPager.OnPageChangeListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ViewPager billViewPager;
    private BillListAdapter billListAdapter;
    private Spinner queryDateSpinner;

    public BillQueryPageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BillListFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BillQueryPageFragment newInstance(String param1, String param2) {
        BillQueryPageFragment fragment = new BillQueryPageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_bill_list, container, false);

        queryDateSpinner = view.findViewById(R.id.queryDate);

        // 年月 选择器
        YearMonthAdapter yearMonthAdapter = new YearMonthAdapter(getContext());

        queryDateSpinner.setAdapter(yearMonthAdapter);


        billViewPager = view.findViewById(R.id.billViewPager);


        billListAdapter = new BillListAdapter(
                getParentFragmentManager(),
                yearMonthAdapter::getItem
        );

        billViewPager.setAdapter(billListAdapter);

        billViewPager.addOnPageChangeListener(this);

        queryDateSpinner.setOnItemSelectedListener(this);
        queryDateSpinner.setSelection(0);
        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //  Spinner 选择框切换  触发 ViewPager Fragment 页面切换

        billViewPager.setCurrentItem(position);

    }



    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        // ViewPager Fragment 页面切换 触发 Spinner 选择框切换
        queryDateSpinner.setSelection(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}